package com.travel.member.web;

import com.alibaba.fastjson.JSON;
import com.travel.common.to.TicketTo;
import com.travel.common.vo.MemberRespVo;
import com.travel.member.service.TicketCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

import static com.travel.common.constant.AuthServerConstant.LOGIN_USER;

@Controller
public class PageController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    TicketCollectionService ticketCollectionService;

    @GetMapping("/sign")
    public String sign(){
        return "sign";
    }

    @GetMapping("/myTicketCollection")
    public String myTicketCollection(Model model){

        //1. 从reids中获取当前用户
        String user = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo loginUser = JSON.parseObject(user, MemberRespVo.class);
        model.addAttribute("loginUser",loginUser);

        //2. 获取收藏的票务信息
        List<TicketTo> myTicketCollections = ticketCollectionService.getMyTicketCollection(loginUser.getMemberId());
        model.addAttribute("myTicketCollections",myTicketCollections);

        return "myTicketCollection";
    }
}
